<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddProductsUnionPk extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 创建id+menuId联合主键
        \Illuminate\Support\Facades\DB::statement("ALTER TABLE `kf_products` 
DROP PRIMARY KEY,
ADD PRIMARY KEY (`id`, `menuId`) USING BTREE;");

        // 把所有pid迁移到id
        \App\Kuafu\Product\Model\Product::query()->where('pid', "!=", 0)
                                        ->update(['id' => \Illuminate\Support\Facades\DB::raw('pid')]);

        Schema::table('products', function (Blueprint $table) {
            $table->dropColumn('pid');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('products', function (Blueprint $table) {
            $table->integer('pid')->default(0);
        });
    }
}
